package soccerscope.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import soccerscope.db.DatabaseReader;
import soccerscope.db.GameItem;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author Linh Phong Le
 * @version 1.0
 */
public class LoadGameDialog
    extends JDialog {

  /**
   * 
   */
  private static final long serialVersionUID = 5077383860070758450L;

  public LoadGameDialog() {

    this.setSelectedGame(new GameItem(-1, null));
    this.loadGameName();

    try {
      this.jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void setSelectedGame(GameItem selectedGame) {

    this.selectedGame = selectedGame;
  }

  public GameItem getSelectedGame() {

    return this.selectedGame;
  }

  ArrayList<GameItem> gis;
  JScrollPane         jScrollPane1  = new JScrollPane();
  JList               jGameList     = new JList();
  BorderLayout        borderLayout1 = new BorderLayout();
  JPanel              jPanel1       = new JPanel();
  JButton             jCancelButton = new JButton();
  JButton             jDeleteButton = new JButton();
  JButton             jLoadButton   = new JButton();

  private void jbInit() throws Exception {

    this.jGameList.setModel(this.gameItems);
    this.jLoadButton
        .addActionListener(new LoadGameDialog_jLoadButton_actionAdapter(this));
    this.setModal(true);
    this.jCancelButton
        .addActionListener(new LoadGameDialog_jCancelButton_actionAdapter(this));
    this.jScrollPane1.getViewport().add(this.jGameList);
    this.getContentPane().setLayout(this.borderLayout1);
    this.getContentPane().add(this.jScrollPane1, java.awt.BorderLayout.CENTER);
    this.jCancelButton.setText("Cancel");
    this.getContentPane().add(this.jPanel1, java.awt.BorderLayout.SOUTH);
    this.jDeleteButton.setText("Delete");
    this.jLoadButton.setText("Load");
    this.jPanel1.add(this.jLoadButton);
    this.jPanel1.add(this.jDeleteButton);
    this.jPanel1.add(this.jCancelButton);
    this.setBounds(150, 150, 450, 350);
    this.setTitle("Load game");
  }

  void loadGameName() {

    this.gis = DatabaseReader.getInstance().getGameInfos();
    for (GameItem i : this.gis) {
      this.gameItems.addElement(i.getDescription());
    }

  }

  private DefaultListModel gameItems = new DefaultListModel();

  public void jLoadButton_actionPerformed(ActionEvent e) {

    this.setSelectedGame(this.gis.get(this.jGameList.getSelectedIndex()));
    this.dispose();
  }

  public void jCancelButton_actionPerformed(ActionEvent e) {

    this.setSelectedGame(new GameItem(-1, null));
    this.dispose();
  }

  private GameItem selectedGame;
}

class LoadGameDialog_jCancelButton_actionAdapter
    implements ActionListener {

  private LoadGameDialog adaptee;

  LoadGameDialog_jCancelButton_actionAdapter(
      LoadGameDialog adaptee) {

    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {

    this.adaptee.jCancelButton_actionPerformed(e);
  }
}

class LoadGameDialog_jLoadButton_actionAdapter
    implements ActionListener {

  private LoadGameDialog adaptee;

  LoadGameDialog_jLoadButton_actionAdapter(
      LoadGameDialog adaptee) {

    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {

    this.adaptee.jLoadButton_actionPerformed(e);
  }
}
